package com.example.spring.multi.communication.socket;

import io.vertx.core.net.NetSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

/**
 * @author : <a href="https://iiiii.run">xiaohan.yuan</a>
 * @version :    1.0
 * @description : 关闭socket处理器
 * @date :    2021-09-15 14:07
 */
@Component
@Scope(value = SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
@Slf4j
public class SocketCloseHandler {

    public void handler(NetSocket socket) {
        String socketId = socket.writeHandlerID();
        for (String strKey : SocketStorageDeal.socketMap.keySet()) {
            if (strKey.startsWith(socketId)) {
                log.info("关闭Socket[{}]成功!", strKey);
                SocketStorageDeal.removeNetSocket(strKey);
            }
        }
        log.info("关闭socket[{}]成功!", socketId);
    }
}
